package com.spring.data.jpa.demo.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.data.jpa.demo.core.dao.TeamDao;
import com.spring.data.jpa.demo.core.entity.Team;
import com.spring.data.jpa.demo.core.service.TeamService;

@Service
@Transactional
public class TeamServiceImpl implements TeamService {

	@Autowired
	private TeamDao teamDao;

	@Override
	public void addTeam(Team team) {
		teamDao.save(team);
	}

	@Override
	public void updateTeam(Team team) {
		Team teamToUpdate = getTeam(team.getId());
		teamToUpdate.setName(team.getName());
		teamToUpdate.setRating(team.getRating());
	}

	@Override
	public Team getTeam(Integer id) {
		return teamDao.findOne(id);
	}

	@Override
	public void deleteTeam(Integer id) {
		Team teamToDelete = getTeam(id);
		if (teamToDelete == null) {
			return;
		}
		teamDao.delete(teamToDelete);
	}

	@Override
	public List<Team> getTeams() {
		return teamDao.findAll();
	}

}
